package com.student_system.students.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.student_system.common.Result;
import com.student_system.students.entity.Student;
import com.student_system.students.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljy
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/student")
public class StudentController {
   @Autowired
   IStudentService studentService;


    @GetMapping("/pageStudent")
    public Page<Student> pageStudent(@RequestParam(required = false) String studentNo,
                                  @RequestParam(required = false) Integer current,
                                  @RequestParam(required = false) Integer size){

        if(current == null){
            current = 1;
        }
        if (size == null){
            size = 10;
        }
        //创建分页对象，将当前页和每页显示的数量传进去(因为mybatiesPlus的分页插件只支持Page对象)
        Page<Student> page = new Page<>(current,size);
        // 调用service方法，查询角色列表，并将分页对象和查询条件传进去
        Page<Student> studentPage = studentService.queryStudentPage(page,studentNo);

        return studentPage;
    }

    /**
     * 新增一个角色
     * @param student Student
     * @return String
     */
    @PostMapping("/addStudent")
    public String addStudent(@RequestBody Student student){
        /**相当于insert into sys_role (role_name,description) value(?,?)*/
        studentService.save(student);
        return "success";
    }

    /**
     * 删除角色信息
     */
    @DeleteMapping("/delStudent/{studentNo}")
    public String delStudent(@PathVariable Integer studentNo){
        studentService.removeById(studentNo);
        return "success";
    }

    /**
     * 查询角色信息
     */
    @GetMapping("/selectStudent/{studentNo}")
    public Student selectStudent(@PathVariable Integer studentNo){
        /** 相当于select * from sys_role  where role_id =? */
        Student student  = studentService.getById(studentNo);
        return student;
    }

    //查询角色列表
    @GetMapping("/searchStudent")
    public Object searchstudent(
            @RequestParam(required = false) Integer studentNo,
            @RequestParam(required = false) String name) {

        if (studentNo != null) {
            // 优先按ID精确查询
            return studentService.getById(studentNo);
        }

        // 按名称模糊查询
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (name != null) {
            queryWrapper.like("name", name);
        }

        List<Student> students = studentService.list(queryWrapper);

        // 如果只查询到一条记录，直接返回该对象
        if (students.size() == 1) {
            return students.get(0);
        }

        return students;
    }

    //修改一个角色
    @PutMapping("/updateStudent")
    public String updateStudent(@RequestBody Student student){
        studentService.updateById(student);
        return "success";
    }




}